package com.springboot.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.springboot.service.AdminService;

@RestController
@RequestMapping(path = "/AdminController" , method = RequestMethod.POST)
public class AdminController {
	@Autowired
	AdminService adminService;
	
	//登录
	@RequestMapping("/getAdminByIdByPass")
	public Integer getAdminByIdByPass(String adminName, String password) throws Exception{
		return adminService.getAdminByIdByPass(adminName, password);
	}
	
	//删除管理员
	@RequestMapping("/deleteAdmin")
	public int deleteAdmin(String adminName, String password) {
		//1.验证密码是否正确
		Integer adminId = adminService.getAdminByIdByPass(adminName, password);
		//2.删除
		if(adminId != null && adminId > 0) {//找到了id
			return adminService.deleteAdmin(adminName, password);
		}
		return 0;
	}
	
	//接收旧密码，两个新密码，一个用户名
	@RequestMapping("/updateAdminOfPass")
	public int updateAdminOfPass(String adminName, String oldPass , String newPass1 , String newPass2) {
		//1.验证密码是否正确
		Integer adminId = adminService.getAdminByIdByPass(adminName, oldPass);
		//2.两次新密码一样吗
		int label = 0;
		if(newPass1.equals(newPass2)) {//新密码合法
			label = 1;
		}
		//3.修改密码
		if(adminId != null && adminId > 0) {
			if(label == 1) {//条件都满足
				return adminService.updateAdminOfPass(adminName, oldPass , newPass1);
			}else {
				return -1;//代表两次密码不一样
			}
		}else
			return 0;//旧密码不对
	}
	
	@RequestMapping("/updateAdminOfName")
	public int updateAdminOfName(String oldName, String newName , String password) {
		//注意：管理员更加严格，需要再输密码（用户登录之后就不用了）
		Integer adminId = adminService.getAdminByIdByPass(oldName, password);
		//2.修改名称
		if(adminId != null && adminId > 0) {//找到了id
			return adminService.updateAdminOfName(adminId , newName);
		}
		return 0;
	}
	
}
